<?php
namespace frontend\models;

use yii\base\Model;
use common\models\member\Member;
use yii\captcha\Captcha;
use common\models\Setting\Setting;
use common\models\adminlog\AdminLog;
/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
  //  public $email;
    public $password;
    public $phone;
	public $verifyCode;
    /**
     * @inheritdoc
     */
    public function rules()
    {
		$setting = Setting::findBySql('SELECT value FROM setting where id = 51')->asArray()->all();
		$data = $setting[0]['value'];
		if(strstr($data,"1")){
			return [
				['username', 'filter', 'filter' => 'trim'],
				['username', 'required'],
				['phone', 'required'],
				['username', 'string', 'min' => 2, 'max' => 255],
				['password', 'required'],
				['password', 'string', 'min' => 6],
				[['verifyCode'], 'required'],
				['verifyCode', 'captcha', 'message'=>'验证码错误','captchaAction'=>'auth/captcha'],
			];
			
		}else{
			return [
				['username', 'filter', 'filter' => 'trim'],
				['username', 'required'],
				['phone', 'required'],
				['username', 'string', 'min' => 2, 'max' => 255],
				['password', 'required'],
				['password', 'string', 'min' => 6],
			];
		}
    }
    public function attributeLabels()
    {
        return [
			'verifyCode' => '', 
        ];
    }
    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
		$adlog = new AdminLog();
        $user = new Member();
        $user->username = $this->username;
        $user->phone = $this->phone;
		    $user->ip = $adlog->real_ip();
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->company = $this->qiyename;
        $user->xingming = $this->xingming;
        $user->work = $this->zhiwu;
        $user->email = $this->email;
        return $user->save() ? $user : null;
    }
}
